package com.example.add8_17.sql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.add8_17.bean.Sqlbean;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class Sql extends SQLiteOpenHelper {
    private Context context;
    public Sql(Context context) {
        super(context, "name", null, 1);
        this.context=context;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("create table name(id integer primary key autoincrement,title varchar(20))");
    }
    public void nameinsert(String title){
        Sql sql = new Sql(context);
        SQLiteDatabase writableDatabase = sql.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("title",title);
        writableDatabase.insert("name",null,values);
        sql.close();
    }
    public List<Sqlbean> namequery(){
        Sql sql = new Sql(context);
        SQLiteDatabase writableDatabase = sql.getWritableDatabase();
        ArrayList<Sqlbean> sqlbeans = new ArrayList<>();
        Cursor name = writableDatabase.query("name", null, null, null, null, null, null);
        if (name.moveToNext()){
            String title = name.getString(name.getColumnIndex("title"));
            int id = name.getInt(name.getColumnIndex("id"));
            sqlbeans.add(new Sqlbean(id,title));
        }
        sql.close();
        return sqlbeans;
    }
    public void namedelete(int id){
        Sql sql = new Sql(context);
        SQLiteDatabase writableDatabase = sql.getWritableDatabase();
        writableDatabase.delete("name","id=?",new String[]{id+""});
        sql.close();

    }
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}
